'use strict';
(function() {
	var from = (function($) {
		var
		flag = false,
		common = {
			submitButton : $('#submitButton'),
			form : $('#FromForm'),
			status : $("#status").val(),
			init : function() {
				helper.date_ymd(true);
				helper.date_his();
				this.submit();
			},
			submit : function() {
				var _this = this, msg;

				this.submitButton.click(function(e) {
					if (_this.fieldTest()) {

						if (location.href.match('create')) {
							var msg = other.confirmMessage();

							layer.confirm(msg, function() {
								_this.actionSubmit();
							});
						} else {
								_this.actionSubmit();
						}
					}
				});
			},
			fieldTest : function() {
				var inputs = $("input[type='text']");

				if (!flag) {
					inputs.splice(4, 3);
				}

				return helper.detect(inputs);
			},
			actionSubmit : function() {
				var _this = this;

				if ($('#sampledeliver-deliver_time').val()[0] == '_') {
					$('#sampledeliver-deliver_time').val('');
				}
				$.post('/from/' + _this.status, _this.form.serialize(), function(data) {
					if (data == "success") {
						layer.msg('保存成功！');
						window.location.href = "/sample/view-from";
					} else {
						helper.errorPop(data + '请检查网络或联系管理员');
					}
				});
			}
		},
		other = {
			confirmMessage : function() {
				var tube = $('#tube_total').val(),
						dna = $('#dna_total').val(),
						rna = $('#rna_total').val(),
						string = '取样总管数为<font color="red">' + tube + '管</font>，' +
								 '提取DNA管数为<font color="red">' + dna + '管</font>，' +
								 '提取RNA管数为<font color="red">' + rna + '管</font>，填写后不可修改，确认提交吗？';

				return string;
			},
			choose : function() {
				var deliver_btn = $("#sample-deliver-btn"),
						cancel_btn = $("#sample-cancel-btn");

				$(deliver_btn).click(function() {
					$("#sample-deliver").css("display", "inline-table");
					$(cancel_btn).css("display", "inline-table");
					$(this).attr("disabled", "true");
					flag = true;
				});

				$(cancel_btn).click(function() {
					$("#sample-deliver").css("display", "none");
					$(cancel_btn).css("display", "none");
					$(deliver_btn).removeAttr("disabled");
					$(deliver_btn).css("display", "inline-table");
					flag = false;
				});
			},
			dna_id_update : function() {
				$(".dna_id").find("a").click(function() {
					$(this).siblings("input").removeAttr("readonly");
				});

				if ($('#sampledeliver-tube_deliver').val() != '') {
					$("#sample-deliver-btn").click();
				}
			}
		},
		position = {
			init : function() {
				this.click();
			},
			click : function() {
				var _this = this;

				$("#dna_total").change(function() {
					_this.show('dna', this);
				});

				$("#rna_total").change(function() {
					_this.show('rna', this);
				});
			},
			resetClick : function() {
				var _this = this;

				$("#resetBtn-dna").click(function() {
					_this.reset('dna', this);
				});
				$("#resetBtn-rna").click(function() {
					_this.reset('rna', this);
				});
			},
			show : function(type, thisInput) {
				if ($("input[name=dna_id]").val() == '') {
					helper.errorPop('请先填写分子号');
					$(thisInput).val('');
					return ;
				}

				var table_div = $(".position-table." + type),
						string = this.input(type, parseInt(thisInput.value));

				$(thisInput).attr("readonly", "true");
				$(table_div).css("display", "block");
				$(table_div).append(string);
				$(table_div).before('<button id="resetBtn-' + type + '" class="btn btn-primary">重置</button>');
				this.resetClick();
			},
			reset : function(type, btn) {
				var table_div = $(".position-table." + type),
						thisInput = $("#" + type + "_total");

				$(table_div).html('');
				$(table_div).css("display", "none");
				$(thisInput).removeAttr("readonly");
				$(thisInput).val('');
				$(btn).remove();
			},
			input : function(type, ammount) {
				var head = '' +
        						'<table class="table">' +
            					'<thead>' +
            						'<tr align="center">' +
	            						'<td>' + ((type == 'dna') ? 'D' : 'R') + 'NA编号</td>' +
	                				'<td>盒名</td>' +
	                				'<td>A</td>' +
	                				'<td>B</td>' +
	                				'<td>X</td>' +
	                				'<td>Y</td>' +
	                				'<td>Z</td>' +
	                			'</tr>' +
	                		'</thead>';

	      var end = "</table>";
				var result = head;

				for (var i = 0; i < ammount; i ++) {
					result += this.string(type, i+1);
				}

				result += end;
				return result;
			},
			string : function(type, i) {
	      var rna_name = ['', 'A', 'B', 'C', 'D', 'E'],
	      		dna_last = parseInt($('#dna_total_last').val()),
	      		rna_last = parseInt($('#rna_total_last').val()),
	      		index = (type == 'dna') ? (dna_last + i) : rna_name[rna_last + i],
	      		xna_id = $("input[name=dna_id]").val() + '-' + index,
						string = '<tr align="center">' + 
									'<td><input type="hidden" name="xna_type[]" value="' + type + '">' +
									'<input type="hidden" name="xna_id[]" value="' + xna_id + '">' + xna_id + '</td>' +
									'<td><input type="text" name="case[]"></td>' +
									'<td><input type="text" name="case_a[]"></td>' +
									'<td><input type="text" name="case_b[]"></td>' +
									'<td><input type="text" name="xna_x[]"></td>' +
									'<td><input type="text" name="xna_y[]"></td>' +
									'<td><input type="text" name="xna_z[]"></td>' +
								'</tr>';
				return string;
			}
		},
		init = function() {
			other.choose();
			common.init();
			position.init();
			other.dna_id_update();
		};
		return {
			init : init
		};
	})(jQuery);

	from.init();
})();